package com.powerunion.datacollection.web.struts;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.powerunion.datacollection.service.ActivityManager;
import com.powerunion.datacollection.service.ApproveWorkItemManager;
import com.powerunion.datacollection.service.CategoryRolesManager;
import com.powerunion.datacollection.service.SubmissionWorkItemManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class CategoryRolesAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(CategoryRolesAction.class);
	private  CategoryRolesManager crManager = null;
	
	public CategoryRolesAction() {
		super();
		this.crManager = new CategoryRolesManager();
	}
	
	public void finalize() throws Throwable {
		this.crManager = null;
		super.finalize();
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String saveStr = request.getParameter("saveStr");
		String roles = request.getParameter("roles");
		
		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("权限信息定义:"+saveStr);
			logger.debug("角色信息定义:"+roles);
			
			boolean flag = this.crManager.save(saveStr, roles);
			
	        if(flag) {
	        	response.getWriter().write("{success:true}");
	        }else{
	        	response.getWriter().write("{failure:true}");
	        }
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}

		return null;
	}
	
	/**
	 * 根据类别ID查找对应的角色ID
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getRolesByCatetory (ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		//得到前台传来的id值查询相应的数据，并返回json格式的数据给前台
		String categoryId = request.getParameter("categoryId");
		int permissionType = Integer.parseInt(request.getParameter("permissionType"));
		StringBuffer data = new StringBuffer();

		JSONArray jsonArray = this.crManager.getRolesByCatetoryId(categoryId, permissionType);
		data.append("{success:true,data:").append(jsonArray).append(" }");

		String str = data.toString();
		System.out.println("ID为:" + categoryId + "的角色信息为" + str);
		try {
			response.getWriter().print(str);
		} catch (IOException e) {
			logger.error("输出ID为："+categoryId+"的角色信息失败" ,e);
			return null;
		}
		
		return null;
	}
	

	
}
